package wfl.spec.edu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ChangeActivity extends AppCompatActivity {
    private static final String TAG ="ChangeActivity" ;
    private EditText StudentNumber; // 帐号编辑框
    private EditText Psd; // 密码编辑框
    private EditText Psd2; // 密码编辑框

    private String Result;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.changepsw);
        StudentNumber = (EditText) findViewById(R.id.change_studentNumber);
        Psd = (EditText) findViewById(R.id.change_password);
        Psd2 = (EditText) findViewById(R.id.change_password2);
    }

    public void finish(View v) {
        sendRequestWihtOkhttp();
        if(Result == "true" && Psd.getText().toString().equals(Psd2.getText().toString()) )
        {
            Toast.makeText(ChangeActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(ChangeActivity.this,ActivityLogin.class);
            startActivity(intent);
        }
        if(Result == "false")
        {
            Toast.makeText(ChangeActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
            StudentNumber.setText(null);
            Psd.setText(null);
            Psd2.setText(null);
        }
        if(!Psd.getText().toString().equals(Psd2.getText().toString()) ) {
            Toast.makeText(ChangeActivity.this, "前后密码不一致", Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * 发起网络请求
     */
    private void sendRequestWihtOkhttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 构造客户端，相当于浏览器
                    OkHttpClient client = new OkHttpClient();
                    // 构造请求，相当于在浏览器里输入地址
                    Request request = new Request
                            .Builder()
                            .url("http://www.yyjsx.xyz/api/student/updatePassword"+"?studentNumber="+StudentNumber.getText().toString()+"&password="+Psd.getText().toString())
                            .build();
                    // 发送请求，相当于在浏览器按回车
                    Response response = client.newCall(request).execute();
                    // 更新ListView
                    String body = response.body().string();
//                    Log.d(TAG, body);
                    parseJson(body);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void parseJson(String jsonStr){
        try {
            JSONObject jobj = new JSONObject(jsonStr);
            Result = jobj.getString("success");
            Log.d(TAG,Result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}